#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "address_book_system.h"
//通讯录管理系统，包括添加联系人，显示联系人，删除联系人，查找联系人，修改联系人，清空联系人

//添加联系人：向通讯录中添加新人
void Add_Contact(int usernum)
{
    FILE* fp;
    seqlist_t writeuser;
    seqlist_t* w=&writeuser;
    int choose=0;

    fp=fopen("userdata.bin","r+");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }
    printf("usernum:%d\n",usernum);
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&writeuser,sizeof(seqlist_t),1,fp);

    w=CreateEpSeqlist();

    printf("\n");
    printf("*************************\n");
    while (1)
    {
        choose=0;
        printf("请输入要添加的联系人的信息:\n");
        printf("请输入姓名:\n");
        scanf("%s",writeuser.data[writeuser.last].name);
        printf("请输入性别(0代表男,1代表女):\n");
        scanf("%d",&writeuser.data[writeuser.last].sex);
        printf("请输入年龄:\n");
        scanf("%d",&writeuser.data[writeuser.last].age);
        printf("请输入电话号码:\n");
        scanf("%s",writeuser.data[writeuser.last].tel);
        printf("请输入地址:\n");
        scanf("%s",writeuser.data[writeuser.last].address);
        writeuser.last++;
        printf("是否继续添加(是输入1):\n");
        scanf("%d",&choose);
        if (choose==1)
        {
            continue;
        }
        break;
    }

    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fwrite(&writeuser,sizeof(seqlist_t),1,fp);
    
    fclose(fp);
    free(w);
}

//显示联系人
void Show_Contact(int usernum)
{
    FILE* fp;
    seqlist_t readuser;
    int i=0;

    fp=fopen("userdata.bin","r");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&readuser,sizeof(seqlist_t),1,fp);
    while (i!=readuser.last)
    {
        printf("\n");
        printf("************************\n");
        printf("姓名:%s\n",readuser.data[i].name);
        printf("性别:%d\n",readuser.data[i].sex);
        printf("年龄:%d\n",readuser.data[i].age);
        printf("电话号码:%s\n",readuser.data[i].tel);
        printf("地址:%s\n",readuser.data[i].address);
        printf("************************\n");
        printf("\n");
        i++;
    }
    
    fclose(fp);
}

//删除联系人
void Delete_Contact(int usernum)
{
    FILE* fp;
    seqlist_t readuser;
    seqlist_t* r=&readuser;
    char username[32]={'\0'};
    int i=0;
    int flag=0;

    r=CreateEpSeqlist();

    fp=fopen("userdata.bin","r+");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }

    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&readuser,sizeof(seqlist_t),1,fp);
    printf("\n");
    printf("************************\n");
    printf("请输入要删除的联系人:\n");
    scanf("%s",username);
    while (i!=readuser.last)
    {
        if(strcmp(username,readuser.data[i].name)==0)
        {
            flag=1;
            break;
        }
        i++;
    }
    if (flag==1)
    {
        while (i!=readuser.last)
        {
            readuser.data[i]=readuser.data[i+1];
            i++;
        }
        readuser.last--;
    }
    else
    {
        printf("删除失败!\n");
        return;
    }
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fwrite(&readuser,sizeof(seqlist_t),1,fp);
    printf("删除成功!\n");
    printf("************************\n");
    printf("\n");

    fclose(fp);
    free(r);
}

//查找联系人
void Find_Contact(int usernum)
{
    FILE* fp;
    seqlist_t readuser;
    int i=0;
    char username[32]={'\0'};

    fp=fopen("userdata.bin","r");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&readuser,sizeof(seqlist_t),1,fp);
    
    printf("\n");
    printf("************************\n");
    printf("请输入要查找的联系人:\n");
    scanf("%s",username);
    while (i!=readuser.last)
    {
        if(strcmp(username,readuser.data[i].name)==0)
        {
            printf("姓名:%s\n",readuser.data[i].name);
            printf("性别:%d\n",readuser.data[i].sex);
            printf("年龄:%d\n",readuser.data[i].age);
            printf("电话号码:%s\n",readuser.data[i].tel);
            printf("地址:%s\n",readuser.data[i].address);
            printf("************************\n");
            printf("\n");
            fclose(fp);
            return;
        }
        i++;
    }
    printf("未查找到联系人!\n");
    printf("************************\n");
    printf("\n");

    fclose(fp);
}

//修改联系人
void Change_Contact(int usernum)
{
    FILE* fp;
    seqlist_t readuser;
    seqlist_t* r=&readuser;
    char username[32]={'\0'};
    int i=0;
    int flag=0;

    r=CreateEpSeqlist();

    fp=fopen("userdata.bin","r+");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }

    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&readuser,sizeof(seqlist_t),1,fp);

    printf("\n");
    printf("*************************\n");

    printf("请输入要修改的联系人:\n");
    scanf("%s",username);

    while (i!=readuser.last)
    {
        if(strcmp(username,readuser.data[i].name)==0)
        {
            printf("请输入要修改的联系人的信息:\n");
            printf("请输入姓名:\n");
            scanf("%s",readuser.data[i].name);
            printf("请输入性别(0代表男,1代表女):\n");
            scanf("%d",&readuser.data[i].sex);
            printf("请输入年龄:\n");
            scanf("%d",&readuser.data[i].age);
            printf("请输入电话号码:\n");
            scanf("%s",readuser.data[i].tel);
            printf("请输入地址:\n");
            scanf("%s",readuser.data[i].address);

            fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
            fwrite(&readuser,sizeof(seqlist_t),1,fp);
            
            printf("修改成功!\n");
            flag=1;
            break;
        }
        i++;
    }
    if (flag==0)
    {
        printf("未查找到该联系人!\n");
    }
    printf("*************************\n");
    printf("\n");
    free(r);
    fclose(fp);
}

//清空联系人
void Clear_Contact(int usernum)
{
    FILE* fp;
    seqlist_t readuser;
    seqlist_t* r=&readuser;
    int i=0;

    r=CreateEpSeqlist();

    fp=fopen("userdata.bin","r+");//打开存储文件
    if (NULL==fp)
    {
        perror("user fopen err\n");
        return;
    }
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fread(&readuser,sizeof(seqlist_t),1,fp);
    while (i!=readuser.last)
    {
        readuser.data[i]=readuser.data[readuser.last+1];
        i++;
    }
    readuser.last=0;
    fseek(fp,usernum*sizeof(seqlist_t),SEEK_SET);
    fwrite(&readuser,sizeof(seqlist_t),1,fp);
    free(r);
    fclose(fp);
}